﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EITSurferInfinityLib;
using System.IO;

namespace ControlerLib
{
    /// <summary>
    /// Interaction logic for UserControlSerialConnection.xaml
    /// </summary>
    public partial class UserControlSerialConnection : UserControl
    {
        VirtualDevice Device;
        string Target;


        public UserControlSerialConnection(ref VirtualDevice device, string target)
        {
            InitializeComponent();
            Device=device;
            Target = target;
        }

        private void Popolate_box(string path)
        {

            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader(path);
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(Target))
                {
                    string [] splitted = line.Split(' ');
                    textBox_baud.Text=splitted[2];
                    textBox_comport.Text=splitted[1];
                }
            }

            file.Close();
        }

        private void Save_box(string path)
        {

            string line;
            List<string> lines = new List<string>();

            int counter = 0;
            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader(path);
            while ((line = file.ReadLine()) != null)
            {
                lines.Add(line);
                if (line.Contains(Target))
                {
                    string splitted = Target+" ";
                    splitted += textBox_comport.Text+" ";
                    splitted += textBox_baud.Text;
                    lines[lines.Count-1]=splitted;
                }
                else
                {
                    //lines.Add(line);
                }
                counter++;
            }
            file.Close();

            System.IO.File.WriteAllLines(path, lines);
        }

        private void button_connect_Click(object sender, RoutedEventArgs e)
        {
            Device.SerialOpen(Target, textBox_comport.Text, textBox_baud.Text);
            string openport=Device.SerialIsOpen();
            if (openport.Contains(Target))
            {
                button_connect.IsEnabled = false;
                button_disconnect.IsEnabled = true;
                Save_box(@"hardware settings\misc\serial.txt");
            }
        }

        public void SetPortName(string name)
        {
            textBox_comport.Text = name;
        }
        public void SetBaud(string rate)
        {
            textBox_baud.Text = rate;
        }

        private void button_id_Click(object sender, RoutedEventArgs e)
        {
            Send("id");
        }

        private void Send(string msg)
        {
            Device.SerialSend(Target, msg);
        }

        private void button_disconnect_Click(object sender, RoutedEventArgs e)
        {
            Device.SerialClose(Target);
            button_connect.IsEnabled = true;
            button_disconnect.IsEnabled = false;
        }

        private void groupBox1_Loaded(object sender, RoutedEventArgs e)
        {
            Popolate_box(@"hardware settings\misc\serial.txt");
        }

        private void UserControl_GotFocus(object sender, RoutedEventArgs e)
        {
            string openport = Device.SerialIsOpen();
            if (openport.Contains(Target))
            {
                button_connect.IsEnabled = false;
                button_disconnect.IsEnabled = true;
            }
        }
    }
}
